package com.yazi.mxz.person.controller.firm;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;




import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.jackson.result.ListResult;
import com.yazi.mxz.common.entity.beans.Store;
import com.yazi.mxz.common.entity.custom.GoodsReportView;
import com.yazi.mxz.common.service.GoodsReportViewService;
import com.yazi.mxz.common.service.StoreService;
import com.yazi.mxz.person.jackson.filter.GoodsReportViewFilter;

@Controller("firm-goodsReportController")
@RequestMapping("/firm/goodsReport")
public class GoodsReportController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(GoodsReportController.class);
	
	@Resource
	GoodsReportViewService goodsReportViewService;
	@Resource
	StoreService storeService;
	
	@RequestMapping
	public String index(Model model) {
		debug(log, "进入企业报表页面");
		Set<Store> stores = getCompany(true).getStores();
		model.addAttribute("stores", stores);
		return "firm/goodsReport";
	}
	
	@RequestMapping("/stores/json")
	@ResponseBody
	public ListResult<?> storesJson(long startTime, long endTime) {
		debug(log, "获取企业各店铺营业额报表数据");
		ListResult<GoodsReportView> jsonResult = new ListResult<GoodsReportView>();
		jsonResult.setFilter(GoodsReportViewFilter.get());
		try {
			List<GoodsReportView> list = goodsReportViewService.listByCompanyId4Store(getCompanyId(), startTime, endTime, null);
			jsonResult.setResult(list);
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "获取企业各店铺营业额报表数据异常");
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	@RequestMapping("/revenueGoodses/json")
	@ResponseBody
	public ListResult<?> revenueGoodsesJson(long startTime, long endTime, Long storeId) {
		debug(log, "获取某时间段内各商品销售额,成本数据");
		ListResult<GoodsReportView> jsonResult = new ListResult<GoodsReportView>();
		jsonResult.setFilter(GoodsReportViewFilter.get());
		List<GoodsReportView> list;
		try {
			if (null != storeId) {
				Store store = storeService.one(storeId);
				// 非企业店铺直接忽略
				if (null == store || !store.getCompany().equals(getCompany(true))) {
					jsonResult.setFail();
					return jsonResult;
				}
				list = goodsReportViewService.listRevenueByStoreId4Goods(storeId, startTime, endTime, null);
			} else {
				list = goodsReportViewService.listRevenueByCompanyId4Goods(getCompanyId(), startTime, endTime, null);
			}
			
			jsonResult.setResult(list);
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "获取某时间段内各商品销售额,成本数据异常");
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	

	@RequestMapping("/lossGoodses/json")
	@ResponseBody
	public ListResult<?> lossGoodsesJson(long startTime, long endTime, Long storeId) {
		debug(log, "获取某时间段内各商品损耗数据");
		ListResult<GoodsReportView> jsonResult = new ListResult<GoodsReportView>();
		jsonResult.setFilter(GoodsReportViewFilter.get());
		List<GoodsReportView> list;
		try {
			if (null != storeId) {
				Store store = storeService.one(storeId);
				// 非企业店铺直接忽略
				if (null == store || !store.getCompany().equals(getCompany(true))) {
					jsonResult.setFail();
					return jsonResult;
				}
				list = goodsReportViewService.listLossByStoreId4Goods(storeId, startTime, endTime, null);
			} else {
				list = goodsReportViewService.listLossByCompanyId4Goods(getCompanyId(), startTime, endTime, null);
			}
			
			jsonResult.setResult(list);
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "获取某时间段内各商品损耗数据异常");
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	@RequestMapping("/sum/json")
	@ResponseBody
	public ListResult<?> sumJson(long startTime, long endTime, Long storeId) {
		debug(log, "某时间段内营业额,成本,损耗数据");
		ListResult<GoodsReportView> jsonResult = new ListResult<GoodsReportView>();
		jsonResult.setFilter(GoodsReportViewFilter.get());
		List<GoodsReportView> list;
		try {
			if (null != storeId) {
				Store store = storeService.one(storeId);
				// 非企业店铺直接忽略
				if (null == store || !store.getCompany().equals(getCompany(true))) {
					jsonResult.setFail();
					return jsonResult;
				}
				list = goodsReportViewService.sumByStoreId(storeId, startTime, endTime);
			} else {
				list = goodsReportViewService.sumByCompanyId(getCompanyId(), startTime, endTime);
			}
			
			jsonResult.setResult(list);
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "某时间段内营业额,成本,损耗数据异常");
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	@RequestMapping("/days/json")
	@ResponseBody
	public ListResult<?> dayJson(long startTime, long endTime, Long storeId) {
		debug(log, "某时间段内营业额走势(按天)数据");
		ListResult<GoodsReportView> jsonResult = new ListResult<GoodsReportView>();
		jsonResult.setFilter(GoodsReportViewFilter.get());
		List<GoodsReportView> list;
		try {
			if (null != storeId) {
				Store store = storeService.one(storeId);
				// 非企业店铺直接忽略
				if (null == store || !store.getCompany().equals(getCompany(true))) {
					jsonResult.setFail();
					return jsonResult;
				}
				list = goodsReportViewService.listRevenueByStoreId4Day(storeId, startTime, endTime, null);
			} else {
				list = goodsReportViewService.listRevenueByCompanyId4Day(getCompanyId(), startTime, endTime, null);
			}
			
			jsonResult.setResult(list);
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "某时间段内营业额走势(按天)数据异常");
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
}
